package org.example.myleet.p541;

public class Solution {
    public String reverseStr(String s, int k) {
        char[] arr = s.toCharArray();
        for (int i = 0; i < arr.length; i = i + 2 * k) {
            if (arr.length - i < k) {
                reverse(arr, i, arr.length - 1);
            } else {
                reverse(arr, i, i + k - 1);
            }
        }
        return new String(arr);
    }

    /**
     * 翻转指定区域的字符串，左(start)闭右(end)闭
     */
    public void reverse(char[] arr, int start, int end) {
        while (start < end) {
            char t = arr[start];
            arr[start] = arr[end];
            arr[end] = t;
            ++start;
            --end;
        }
    }
}
